package com.longe.controller;

import com.longe.export.DateSQLFieldTransform;
import com.longe.export.ExcelExport;
import com.longe.export.SQLField;
import com.longe.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class ExportController {

    @RequestMapping(value = "export")
    public void exportUser(HttpServletResponse response) throws IOException {

        List<UserVO> list = new ArrayList<>();
        List<SQLField> fieldList = new ArrayList<>();
        try {
            list.add(new UserVO("jack", 20, new Date()));
            list.add(new UserVO("tom", 23, new Date()));
        } catch (Exception e) {
            log.error("导出[user]数据异常{}", e);
        } finally {
            fieldList.add(new SQLField("name", "姓名"));
            fieldList.add(new SQLField("age", "年龄"));
            fieldList.add(new SQLField("time", "时间",new DateSQLFieldTransform()));
        }

        response.setHeader("Content-disposition", "attachment; filename=userList.xlsx");
        ExcelExport.exportList(list, fieldList, response.getOutputStream());
    }
}
